#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from typing import List, Optional
from app.dao.project_dao import ProjectQueryDao, ProjectOperateDao
from app.dao.models.mysql_gen import Project
from app.types.apiproto import project_proto


class ProjectService:
    """项目管理服务"""

    @classmethod
    def create_project(cls, req: project_proto.ProjectCreate) -> project_proto.ProjectDetail:
        """创建项目"""
        project = Project(
            project_number=req.project_number,
            project_type=req.project_type,
            funding_amount=req.funding_amount,
            start_date=req.start_date,
            end_date=req.end_date,
            status=req.status
        )
        
        created_project = ProjectOperateDao.create_project(project)
        
        return project_proto.ProjectDetail(
            id=created_project.id,
            project_number=created_project.project_number,
            project_type=created_project.project_type,
            funding_amount=created_project.funding_amount,
            start_date=created_project.start_date,
            end_date=created_project.end_date,
            status=created_project.status,
            created_at=created_project.created_at,
            updated_at=created_project.updated_at
        )

    @classmethod
    def update_project(cls, req: project_proto.ProjectUpdate) -> bool:
        """更新项目"""
        return ProjectOperateDao.update_project(
            project_id=req.id,
            project_number=req.project_number,
            project_type=req.project_type,
            funding_amount=req.funding_amount,
            start_date=req.start_date,
            end_date=req.end_date,
            status=req.status
        )

    @classmethod
    def get_project_by_id(cls, project_id: str) -> Optional[project_proto.ProjectDetail]:
        """根据ID获取项目详情"""
        project = ProjectQueryDao.get_by_id(project_id)
        if not project:
            return None
            
        return project_proto.ProjectDetail(
            id=project.id,
            project_number=project.project_number,
            project_type=project.project_type,
            funding_amount=project.funding_amount,
            start_date=project.start_date,
            end_date=project.end_date,
            status=project.status,
            created_at=project.created_at,
            updated_at=project.updated_at
        )

    @classmethod
    def list_projects(cls, page: int = 1, page_size: int = 10, project_number: str = "", 
                      project_type: str = "", status: str = "") -> project_proto.ProjectListResponse:
        """获取项目列表"""
        # 构造查询参数
        query_dict = {}
        if project_number:
            query_dict["project_number"] = project_number
        if project_type:
            query_dict["project_type"] = project_type
        if status:
            query_dict["status"] = status
        
        total, projects = ProjectQueryDao.list_projects(page, page_size, **query_dict)
        
        if total == 0:
            return project_proto.ProjectListResponse()
        
        # 转换为响应格式
        records_list: List[project_proto.ProjectDetail] = []
        for project in projects:
            record = project_proto.ProjectDetail(
                id=project.id,
                project_number=project.project_number,
                project_type=project.project_type,
                funding_amount=project.funding_amount,
                start_date=project.start_date,
                end_date=project.end_date,
                status=project.status,
                created_at=project.created_at,
                updated_at=project.updated_at
            )
            records_list.append(record)
        
        return project_proto.ProjectListResponse(record_total=total, record_list=records_list)

    @classmethod
    def delete_project(cls, project_id: str) -> bool:
        """删除项目"""
        return ProjectOperateDao.delete_project(project_id)